/*
 * Created on 2005-7-11
 *
 */
package com.et114.components.ldap4oj.support;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.springframework.dao.DataAccessResourceFailureException;

public class UrlContextSource implements ContextSourceInterface {

    private String ldapUrl = null;

    private Hashtable environment = new Hashtable();

    /**
     * @return Returns the ldapUrl.
     */
    public String getLdapUrl() {
        return ldapUrl;
    }

    /**
     * @param ldapUrl
     *            The ldapUrl to set.
     */
    public void setLdapUrl(String ldapUrl) {
        this.ldapUrl = ldapUrl;
    }

    /**
     * @return Returns the securityAuthentication.
     */
    public String getSecurityAuthentication() {
        return (String) environment.get(Context.SECURITY_AUTHENTICATION);
    }

    /**
     * @param securityAuthentication
     *            The securityAuthentication to set.
     */
    public void setSecurityAuthentication(String securityAuthentication) {
        environment
                .put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
    }

    /**
     * @return Returns the securityCredentials.
     */
    public String getSecurityCredentials() {
        return (String) environment.get(Context.SECURITY_CREDENTIALS);
    }

    /**
     * @param securityCredentials
     *            The securityCredentials to set.
     */
    public void setSecurityCredentials(String securityCredentials) {
        environment.put(Context.SECURITY_CREDENTIALS, securityCredentials);
    }

    /**
     * @return Returns the securityPrincipal.
     */
    public String getSecurityPrincipal() {
        return (String) environment.get(Context.SECURITY_PRINCIPAL);
    }

    /**
     * @param securityPrincipal
     *            The securityPrincipal to set.
     */
    public void setSecurityPrincipal(String securityPrincipal) {
        environment.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
    }

    /**
     * @return Returns the securityProtocol.
     */
    public String getSecurityProtocol() {
        return (String) environment.get(Context.SECURITY_PROTOCOL);
    }

    /**
     * @param securityProtocol
     *            The securityProtocol to set.
     */
    public void setSecurityProtocol(String securityProtocol) {
        environment.put(Context.SECURITY_PROTOCOL, securityProtocol);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.ldap.support.ContextSourceInterface#getDirContext()
     */
    public DirContext getDirContext() {
        try {
            return (DirContext) new InitialDirContext(environment)
                    .lookup(ldapUrl);
        } catch (NamingException ex) {
            throw new DataAccessResourceFailureException(
                    "Cannot retrieve context", ex);
        }
    }


}
